package com.tj.jinzao.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.base.IBaseView;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.easydl.camera.CameraListener;
import com.tj.jinzao.easydl.util.ThreadPoolManager;
import com.tj.jinzao.easydl.view.PreviewView;
import com.tj.jinzao.model.DetectQuestion;
import com.tj.jinzao.presenter.DetectPresenter;
import com.tj.jinzao.utils.Utils;
import com.tj.jinzao.widgets.ChooseConfigDialog;

import java.util.List;

import butterknife.BindView;

/**
 * 舌头识别activity
 * Created by wjy on 2018/5/22.
 */

public class TongueDetectActivity extends BaseActivity implements IBaseView<List<DetectQuestion>> {

    @BindView(R.id.preview_view)
    PreviewView mPreviewView;
    @BindView(R.id.iv_circle)
    ImageView mCircleView;
    @BindView(R.id.iv_result)
    ImageView mResultView;
    @BindView(R.id.detect_result)
    ImageView mDetectView;

    private DetectPresenter mPresenter;

    private boolean mUpload = false;
    private boolean mRunning = true;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStausBar(getResources().getColor(R.color.black));
        start();
        mPresenter = new DetectPresenter(this);
        Animation rotate = AnimationUtils.loadAnimation(this, R.anim.rotate);
        if (rotate != null) {
            mCircleView.startAnimation(rotate);
        }
    }
    private ChooseConfigDialog mDialog;
    private void dialog(String content){
        mDialog = new ChooseConfigDialog(TongueDetectActivity.this, content);
        mDialog.show();
        mDialog.setOnClickLinstener(new ChooseConfigDialog.OnClickLinstener() {
            @Override
            public void onConfig(String mContent) {
                mDialog.dismiss();
                if(mContent.equals("1")){
                    finish();
                }
            }
        });
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if(mDialog!=null&&mDialog.isShowing()){
                return true;
            }
            dialog("退出，检测流程全部终止");
        }
        return true;

    }


    private void start() {
        mPreviewView.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.i("wjy", "start:");
                mPreviewView.start();
                setAutoTakePictureProcess();
            }
        }, 3000);

    }


    private void setAutoTakePictureProcess() {
        ThreadPoolManager.execute(new Runnable() {
            @Override
            public void run() {
                while (mRunning) {
                    synchronized (this) {
                        mPreviewView.takePicture(new CameraListener.takePictureListener() {
                            @Override
                            public void onTakenPicture(Bitmap bitmap) {
                                if (!mUpload) {
                                            String path = Constant.SD_TEMP_IMAGE + System.currentTimeMillis() + ".jpg";
                                            Utils.saveBitmap(path, bitmap);
                                            mUpload = true;
                                            mPresenter.uploadTonguePic(path);
                                }

                            }
                        });
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    @Override
    public Activity getContext() {
        return this;
    }

    @Override
    public void onRequestSuccess(List<DetectQuestion> detectQuestions) {
        startActivity(new Intent(this, DetectChooseActivity.class));
        finish();
    }

    @Override
    public void onRequestNoData(String message) {
        mUpload = false;
    }

    @Override
    public void onRequestError(String error_message) {
        mUpload = false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mRunning = false;

        if (mPreviewView != null) mPreviewView.destory();
        if (mPresenter != null) mPresenter.detachView();
    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_tongue_detect;
    }
}
